package com.sgcc.lms.openstack.service.impl;

import com.sgcc.lms.openstack.exception.NullTokenException;
import com.sgcc.lms.openstack.service.NeutronService;
import com.sgcc.lms.openstack.util.OpenStackClientFactory;
import lombok.extern.slf4j.Slf4j;
import org.openstack4j.api.Builders;
import org.openstack4j.api.OSClient;
import org.openstack4j.model.common.ActionResponse;
import org.openstack4j.model.network.*;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 李孟帅
 * @CreateTime: 2020-07-22 11:06
 * @Description:
 */
@Slf4j
@Service
public class NeutronServiceImpl implements NeutronService {
    private static final String LINE = "==========================================》{}";

    /**
     * @Author 李孟帅
     * @Description 列出网络
     * @Date 2020/7/22
     **/
    @Override
    public List<? extends Network> networks(String tokenId) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        List<? extends Network> list = openStackClient.networking().network().list();
        return list;
    }

    /**
     * @Author 李孟帅
     * @Description 获取网络信息
     * @Date 2020/7/22
     **/
    @Override
    public Network getNetwork(String tokenId, String id) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        Network network = openStackClient.networking().network().get(id);
        return network;
    }

    /**
     * @Author 李孟帅
     * @Description 列出子网
     * @Date 2020/7/22
     **/
    @Override
    public List<? extends Subnet> subnets(String tokenId) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        List<? extends Subnet> list = openStackClient.networking().subnet().list();
        return list;
    }

    /**
     * @Author 李孟帅
     * @Description 获取子网信息
     * @Date 2020/7/22
     **/
    @Override
    public Subnet getSubnet(String tokenId, String id) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        Subnet subnet = openStackClient.networking().subnet().get(id);
        return subnet;
    }

    /**
     * @Author 李孟帅
     * @Description 列出端口
     * @Date 2020/7/22
     **/
    @Override
    public List<? extends Port> ports(String tokenId) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        List<? extends Port> list = openStackClient.networking().port().list();
        return list;
    }

    /**
     * @Author 李孟帅
     * @Description 获取端口信息
     * @Date 2020/7/22
     **/
    @Override
    public Port getPort(String tokenId, String id) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        Port port = openStackClient.networking().port().get(id);
        return port;
    }

    /**
    * @Author 李孟帅
    * @Description 创建端口
    * @Date 2020/7/23
    **/
    @Override
    public Port createPort(String tokenId, Port port) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        Port port1 = openStackClient.networking().port().create(port);
        return port1;
    }

    /**
     * @Author 李孟帅
     * @Description 删除端口
     * @Date 2020/7/22
     **/
    @Override
    public ActionResponse deletePort(String tokenId, String id) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        ActionResponse actionResponse = openStackClient.networking().port().delete(id);
        return actionResponse;
    }

    /**
     * @Author 李孟帅
     * @Description 列出路由器
     * @Date 2020/7/22
     **/
    @Override
    public List<? extends Router> routers(String tokenId) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        List<? extends Router> list = openStackClient.networking().router().list();
        return list;
    }

    /**
     * @Author 李孟帅
     * @Description 获取路由器信息
     * @Date 2020/7/22
     **/
    @Override
    public Router getRouter(String tokenId, String id) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        Router router = openStackClient.networking().router().get(id);
        return router;
    }

    /**
     * @Author 李孟帅
     * @Description 列出安全组
     * @Date 2020/7/23
     **/
    @Override
    public List<? extends SecurityGroup> securityGroups(String tokenId) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        List<? extends SecurityGroup> securityGroups = openStackClient.networking().securitygroup().list();
        return securityGroups;
    }

    /**
     * @Author 李孟帅
     * @Description 获取安全组
     * @Date 2020/7/23
     **/
    @Override
    public SecurityGroup getSecurityGroup(String tokenId, String id) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        SecurityGroup securityGroup = openStackClient.networking().securitygroup().get(id);
        return securityGroup;
    }

    /**
     * @Author 李孟帅
     * @Description 创建安全组
     * @Date 2020/7/23
     **/
    @Override
    public SecurityGroup createSecurityGroup(String tokenId, SecurityGroup securityGroup) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        SecurityGroup securityGroup1 = openStackClient.networking().securitygroup().create(securityGroup);
        return securityGroup1;
    }

    /**
     * @Author 李孟帅
     * @Description 删除安全组
     * @Date 2020/7/23
     **/
    @Override
    public ActionResponse deleteSecurityGroup(String tokenId, String id) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        ActionResponse delete = openStackClient.networking().securitygroup().delete(id);
        return delete;
    }

    /**
     * @Author 李孟帅
     * @Description 更新安全组
     * @Date 2020/7/23
     **/
    @Override
    public SecurityGroup updateSecurityGroup(String tokenId, SecurityGroup securityGroup) throws NullTokenException {
        String id = securityGroup.getId();
        String name = securityGroup.getName();
        String description = securityGroup.getDescription();
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        SecurityGroup update = openStackClient.networking().securitygroup().update(id, Builders.securityGroupUpdate().name(name).description(description).build());
        return update;
    }

    /**
     * @Author 李孟帅
     * @Description 列出安全组规则
     * @Date 2020/7/23
     **/
    @Override
    public List<? extends SecurityGroupRule> securityGroupRules(String tokenId) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        List<? extends SecurityGroupRule> securityGroupRules = openStackClient.networking().securityrule().list();
        return securityGroupRules;
    }

    /**
     * @Author 李孟帅
     * @Description 获取安全组规则
     * @Date 2020/7/23
     **/
    @Override
    public SecurityGroupRule getSecurityGroupRule(String tokenId, String id) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        SecurityGroupRule securityGroupRule = openStackClient.networking().securityrule().get(id);
        return securityGroupRule;
    }

    /**
     * @Author 李孟帅
     * @Description 删除安全组规则
     * @Date 2020/7/23
     **/
    @Override
    public void deleteSecurityGroupRule(String tokenId, String id) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        openStackClient.networking().securityrule().delete(id);

    }

    /**
     * @Author 李孟帅
     * @Description 创建安全组规则
     * @Date 2020/7/23
     **/
    @Override
    public SecurityGroupRule createSecurityGroupRule(String tokenId, SecurityGroupRule securityGroupRule) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        SecurityGroupRule securityGroupRule1 = openStackClient.networking().securityrule().create(securityGroupRule);
        return securityGroupRule1;
    }


}
